package com.achilles.widget;

import android.content.Context;
import android.text.TextUtils;
import android.text.method.TextKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.achilles.bean.DataBean;
import com.achilles.bean.ProjectBean;
import com.achilles.playtask.R;

/**
 * Created by AchillesL on 2016-9-21.
 */
public class CreateProjectView extends DialogView implements View.OnClickListener {

    public static final int NOTE_MAX_LINES_NUMBER = 2;
    private EditText mEdProjectName;
    private EditText mEdProjectScore;
    private EditText mEdTotalTime;
    private RadioGroup mRgProjectType;
    private RadioGroup mRgProjectRepeat;
    private ImageView mIvMore;
    private ImageView mIvFixNote;
    private RelativeLayout mLlMore;
    private EditText mEdNote;
    private HorizontalScrollView mHsProjectType;
    private ProjectBean.ProjectType mProjectType;
    private boolean mIsRepeat;
    private boolean mIsEditMode;
    private boolean mIsFixNote;
    private ProjectBean mEditProjectBean;

    public CreateProjectView(Context context) {
        super(context);
    }

    @Override
    protected int getViewLayout() {
        return R.layout.project_dialog_view;
    }

    @Override
    protected void initView(View contentView) {
        mEdProjectName = (EditText) mContentView.findViewById(R.id.edProjectName);
        mEdProjectScore = (EditText) mContentView.findViewById(R.id.edProjectScore);
        mRgProjectType = (RadioGroup) mContentView.findViewById(R.id.rgProjectType);
        mRgProjectRepeat = (RadioGroup) mContentView.findViewById(R.id.rgProjectRepeat);
        mEdTotalTime = (EditText) mContentView.findViewById(R.id.edTotalTime);
        mIvMore = (ImageView) mContentView.findViewById(R.id.ivMore);
        mIvFixNote = (ImageView) mContentView.findViewById(R.id.ivFixNote);
        mLlMore = (RelativeLayout) mContentView.findViewById(R.id.rlMore);
        mEdNote = (EditText) mContentView.findViewById(R.id.edProjectNote);
        mHsProjectType = (HorizontalScrollView) mContentView.findViewById(R.id.projectTypeScrollView);

        mEdNote.setSingleLine(false);
        mEdNote.setMaxLines(NOTE_MAX_LINES_NUMBER);

        mIvFixNote.setOnClickListener(this);
        mIvMore.setOnClickListener(this);

        mProjectType = ProjectBean.ProjectType.EveryDay;
        mIsRepeat = true;
        mIsFixNote = false;
        mRgProjectType.check(R.id.rbEveryDay);
        mRgProjectRepeat.check(R.id.rbRepeat);
        mRgProjectType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbEveryDay) {
                    mProjectType = ProjectBean.ProjectType.EveryDay;
                } else if (checkedId == R.id.rbEveryWeek) {
                    mProjectType = ProjectBean.ProjectType.EveryWeek;
                } else if (checkedId == R.id.rbEveryMonth) {
                    mProjectType = ProjectBean.ProjectType.EveryMonth;
                } else if (checkedId == R.id.rbNoLimit) {
                    mProjectType = ProjectBean.ProjectType.Normal;
                }
            }
        });
        mRgProjectRepeat.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mIsRepeat = (checkedId == R.id.rbRepeat);
                for(int i = 0;i<mRgProjectType.getChildCount();i++) {
                    mRgProjectType.getChildAt(i).setEnabled(mIsRepeat);
                }
                mEdTotalTime.setEnabled(mIsRepeat);
            }
        });
    }

    @Override
    public void editData(DataBean dataBean) {
        mIsEditMode = true;
        mEditProjectBean = (ProjectBean) dataBean;

        String projectName = mEditProjectBean.getProjectName();
        mEdProjectName.setText(projectName);
        mEdProjectName.setSelection(projectName.length());

        String projectScore = String.valueOf(mEditProjectBean.getProjectScore());
        mEdProjectScore.setText(projectScore);
        mEdProjectScore.setSelection(projectScore.length());

        String projectTotalTime = String.valueOf(mEditProjectBean.getProjectTotalTimes());
        mEdTotalTime.setText(projectTotalTime);
        mEdTotalTime.setSelection(projectTotalTime.length());

        String projectNote = mEditProjectBean.getProjectNote();
        if (!TextUtils.isEmpty(projectNote)) {
            mEdNote.setText(projectNote);
            mEdNote.setSelection(projectNote.length());
            mLlMore.setVisibility(View.VISIBLE);
            mIvMore.setImageResource(R.drawable.ic_action_more_show);
        }

        mIsFixNote = mEditProjectBean.isProjectFixNote();
        mIvFixNote.setImageResource(mIsFixNote ? R.drawable.pin_checked:R.drawable.pin_unchecked);

        ProjectBean.ProjectType projectType = mEditProjectBean.getProjectType();
        if (projectType == ProjectBean.ProjectType.EveryDay) {
            mRgProjectType.check(R.id.rbEveryDay);
            mProjectType = ProjectBean.ProjectType.EveryDay;
        } else if (projectType == ProjectBean.ProjectType.EveryWeek) {
            mRgProjectType.check(R.id.rbEveryWeek);
            mProjectType = ProjectBean.ProjectType.EveryWeek;
        } else if (projectType == ProjectBean.ProjectType.EveryMonth) {
            mRgProjectType.check(R.id.rbEveryMonth);
            mProjectType = ProjectBean.ProjectType.EveryMonth;
        } else if (projectType == ProjectBean.ProjectType.Normal) {
            mRgProjectType.check(R.id.rbNoLimit);
            mProjectType = ProjectBean.ProjectType.Normal;
            mHsProjectType.scrollTo(mHsProjectType.getWidth(),0);
        }

        mIsRepeat = ((ProjectBean) dataBean).getProjectType() != ProjectBean.ProjectType.Single;
        mRgProjectRepeat.check(mIsRepeat ? R.id.rbRepeat : R.id.rbNotRepeat);
    }

    @Override
    public DataBean getData() {
        ProjectBean projectBean;
        if (mIsEditMode) {
            projectBean = mEditProjectBean;
        }else {
            projectBean = new ProjectBean();
            projectBean.setProjectCreateTime(String.valueOf(System.currentTimeMillis()));
        }

        String projectName = mEdProjectName.getText().toString();
        String projectTotalTimeString = mEdTotalTime.getText().toString();
        int projectScore = Integer.parseInt(mEdProjectScore.getText().toString());
        int projectTotalTimes;
        if (TextUtils.isEmpty(projectTotalTimeString)) {
            projectTotalTimes = 1;
        } else {
            projectTotalTimes = Integer.parseInt(projectTotalTimeString);
        }

        if (!mIsRepeat) {
            mProjectType = ProjectBean.ProjectType.Single;
        }
        if (mIsFixNote && mEdNote.length() == 0) {
            mIsFixNote = false;
        }
        projectBean.setProjectFixNote(mIsFixNote);
        projectBean.setProjectNote(mEdNote.getText().toString());
        projectBean.setProjectTotalTimes(projectTotalTimes);
        projectBean.setProjectName(projectName);
        projectBean.setProjectScore(projectScore);
        projectBean.setProjectType(mProjectType);
        projectBean.setProjectOrder(getProjectOrder(projectBean.getProjectType()));

        Log.d("getData",projectBean.toString());
        return projectBean;
    }

    private int getProjectOrder(ProjectBean.ProjectType projectType) {
        int order = 0;
        switch (projectType) {
            case Single:{
                order = 0;
                break;
            }
            case EveryDay:{
                order = 1;
                break;
            }
            case Normal:{
                order = 2;
                break;
            }
            case EveryWeek:{
                order = 3;
                break;
            }
            case EveryMonth:{
                order = 4;
                break;
            }
        }
        return order;
    }

    @Override
    public boolean isParamValid() {
        if (mEdProjectName.length() == 0) {
            mEdProjectName.requestFocus();
            mEdProjectName.setError(getView().getContext().getString(R.string
                    .create_error_name_empty));
            return false;
        }

        if (mEdProjectScore.length() == 0) {
            mEdProjectScore.requestFocus();
            mEdProjectScore.setError(getView().getContext().getString(R.string
                    .create_error_score_empty));
            return false;
        }

        if (mEdTotalTime.length() == 0) {
            mEdTotalTime.requestFocus();
            mEdTotalTime.setError(getView().getContext().getString(R.string
                    .create_error_total_time_empty));
            return false;
        }

        if (Integer.parseInt(mEdProjectScore.getText().toString()) == 0) {
            mEdProjectScore.requestFocus();
            mEdProjectScore.setError(getView().getContext().getString(R.string.create_error_score_is_zero));
            return false;
        }

        if (Integer.parseInt(mEdTotalTime.getText().toString()) == 0) {
            mEdTotalTime.requestFocus();
            mEdTotalTime.setError(getView().getContext().getString(R.string.create_project_error_projectTime_isZero));
            return false;
        }

        if (mIsEditMode) {
            int totalTimeNumber = Integer.parseInt(mEdTotalTime.getText().toString());
            if (totalTimeNumber <= mEditProjectBean.getProjectTimes()) {
                mEdTotalTime.requestFocus();
                mEdTotalTime.setError(getView().getContext().getString(R.string
                        .create_error_totalTime_err));
                return false;
            }
        }
        return true;
    }

    @Override
    public void reset() {
        TextKeyListener.clear(mEdProjectName.getText());
        TextKeyListener.clear(mEdProjectScore.getText());
        TextKeyListener.clear(mEdNote.getText());

        mEdProjectName.requestFocus();
        mEdTotalTime.setText("1");
        mRgProjectType.check(R.id.rbEveryDay);
        mRgProjectRepeat.check(R.id.rbRepeat);
        mIvMore.setImageResource(R.drawable.ic_action_more_hide);
        mIvFixNote.setImageResource(R.drawable.pin_unchecked);
        mLlMore.setVisibility(View.GONE);
        mHsProjectType.scrollTo(0,0);

        mProjectType = ProjectBean.ProjectType.EveryDay;
        mIsRepeat = true;
        mIsEditMode = false;
        mIsFixNote = false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
//        Log.d("onKeyDown","mEdNote.hasFocus(): " + mEdNote.hasFocus());
//        if (mEdNote.hasFocus()) {
//            boolean result = mEdNote.onKeyDown(keyCode, keyEvent);
//            Log.d("onKeyDown","mEdNote.onKeyDown: " + result);
//            return result;
//        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivMore: {
                if (mLlMore.getVisibility() == View.GONE) {
                    mIvMore.setImageResource(R.drawable.ic_action_more_show);
                    mLlMore.setVisibility(View.VISIBLE);
                }else {
                    mIvMore.setImageResource(R.drawable.ic_action_more_hide);
                    mLlMore.setVisibility(View.GONE);
                }
                break;
            }
            case R.id.ivFixNote:{
                mIsFixNote = !mIsFixNote;
                mIvFixNote.setImageResource(mIsFixNote ? R.drawable.pin_checked:R.drawable.pin_unchecked);
                break;
            }
            default:
                break;
        }
    }
}
